package com.heima.behavior.filter;

import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Order(1)
//过滤器名称  拦截所有请求路径
@WebFilter(filterName = "behaviorTokenFilter", urlPatterns = "/*")
@Slf4j
@Component  // 扫描包
public class BehaviorTokenFilter extends GenericFilterBean {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //得到header中的信息
        String userId = request.getHeader("userId");
        //非空判断
        if (userId != null) {
            //创建WmUser对象
            ApUser apUser = new ApUser();
            //将请求头中的userId放入WmUser对象
            apUser.setId(Integer.valueOf(userId));
            // 保存到当前线程中
            AppThreadLocalUtils.setUser(apUser);
        }
        // 如果没有则直接放行
        filterChain.doFilter(request, response);
        // 过滤器处理完毕后  清空用户信息
        AppThreadLocalUtils.clear();
    }
}